import {defHttp} from '/@/utils/http/axios';
import { useMessage } from "/@/hooks/web/useMessage";

const { createConfirm } = useMessage();
const prefix = '/demand/cgcjDemandPlan'

enum Api {
  list = prefix + '/list',
  save = prefix + '/add',
  edit = prefix + '/edit',
  deleteOne = prefix + '/delete',
  deleteBatch = prefix + '/deleteBatch',
  importExcel = prefix + '/importExcel',
  exportXls = prefix + '/exportXls',
  queryDataById = prefix + '/queryById',
  runProcessUrl = prefix + '/runProcess',
  saveDraftUrl = prefix + '/saveDraft',
  queryByTaskIdUrl = prefix + '/queryByTaskId',
  approvalFindNextNodeUserUrl = prefix + '/findNextNodeUserByGeneral',
  submit = prefix + '/submit',
  flowchart = prefix + '/queryProcessByDataCode',
}

/**
 * 导出api
 * @param params
 */
export const getExportUrl = Api.exportXls;

/**
 * 导入api
 */
export const getImportUrl = Api.importExcel;

/**
 * 查询下一节点审批人api
 */
export const approvalFindNextNodeUserUrl = Api.approvalFindNextNodeUserUrl;

/**
 * 列表接口
 * @param params
 */
export const list = (params) => defHttp.get({url: Api.list, params});

/**
 * 删除单个
 * @param params
 * @param handleSuccess
 */
export const deleteOne = (params,handleSuccess) => {
  return defHttp.delete({url: Api.deleteOne, params}, {joinParamsToUrl: true}).then(() => {
    handleSuccess();
  });
}

/**
 * 批量删除
 * @param params
 * @param handleSuccess
 */
export const batchDelete = (params, handleSuccess) => {
  createConfirm({
    iconType: 'warning',
    title: '确认删除',
    content: '是否删除选中数据',
    okText: '确认',
    cancelText: '取消',
    onOk: () => {
      return defHttp.delete({url: Api.deleteBatch, data: params}, {joinParamsToUrl: true}).then(() => {
        handleSuccess();
      });
    }
  });
}

/**
 * 保存或者更新
 * @param params
 * @param isUpdate
 */
export const saveOrUpdate = (params, isUpdate) => {
  let url = isUpdate ? Api.edit : Api.save;
  return defHttp.post({url: url, params});
}

/**
 * 保存并提交
 * @param params
 */
export const submit = (params) => {
  return defHttp.post({url: Api.submit, params});
}

/**
 * 审批
 * @param params
 */
export const saveProcess = (params) => {
  return defHttp.post({url: Api.runProcessUrl, params});
}

/**
 * 保存草稿
 * @param params
 */
export const saveDraft = (params) => {
  return defHttp.post({url: Api.saveDraftUrl, params});
}

/**
 * 通过任务id(待办已办任务或者知会任务id)查询
 * @param params
 */
export const getDataByTaskId = (params) => {
  return defHttp.post({url: Api.queryByTaskIdUrl, params});
}

/**
* 根据id查询数据
* @param id
*/
export const queryDataById = (id) => defHttp.get({url: Api.queryDataById, params:{ id }});

/**
* 查看流程图
*/
export const queryFlowchart = () => defHttp.get({url: Api.flowchart});